<?php
$section = 'booking';

$titre = _LB_BOOKING_MANAGEMENT;
$smarty->assign("titre", $titre);

Session::setBackLink($section);
$filter = array();
if(isset($_GET['STATUS'])){
    $filter["STATUS"] = Request::get('STATUS');
}
else{
    $filter["STATUS"] = -1;
}
if(Request::get('START_DATE') == ""){
	$filter['START_DATE'] = date("Y-m-d");
}
else{
	$filter['START_DATE'] = date_to_date_sql(Request::get('START_DATE'));
}
$filter['ID_SERVICE'] = Request::getIntNumber('SERVICE');
$filter['ID_TECHNICIAN'] = Request::getIntNumber('TECHNICIAN');
$filter['END_DATE'] = date_to_date_sql(Request::get('END_DATE'));
$booking = new Booking();
if($filter["STATUS"] == -1){
    $query = "SELECT * FROM BOOKING ORDER BY ID DESC";
}
else{
    $query = "SELECT * FROM BOOKING WHERE STATUS = ".$filter["STATUS"]." ORDER BY ID DESC";
}
$bookList = Booking_detail::getbookingDetail(0,$filter);
$args = array();
$pager = new  DBPager($booking,$args, false ,PAGING_DEFAULT_ITEM_PER_PAGE,$query);
$booking->query($query);


$technician = new Technician();
$technician->orderBy("TITLE ASC");
$technician->find();

$service = new Service_Booking();
$query = "SELECT SB.*, SG.TITLE TITLE_GROUP FROM SERVICE_GROUP SG "
        . "LEFT JOIN SERVICE_BOOKING SB ON SB.ID_SERVICE_GROUP = SG.ID "
		. "WHERE SB.DISABLED = 0 AND SG.DISABLED = 0 "
        . "ORDER BY SG.TITLE,SG.TITLE ASC";
$service->query($query);
$serviceData = array();
while($service->fetch()){
	if(!isset($serviceData[$service->ID_SERVICE_GROUP])){
		$serviceData[$service->ID_SERVICE_GROUP] = array("TITLE"=>$service->TITLE_GROUP,"DATA"=>array());
	}
	$serviceData[$service->ID_SERVICE_GROUP]["DATA"][$service->ID] = array(
		"ID"=>$service->ID,
		"TITLE"=>$service->TITLE,
		"TIME"=>$service->TIME,
		"PRICE"=>$service->PRICE
	);
}
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_BOOKING_MANAGEMENT => Utils::getCurrentLink()));

if($filter['START_DATE']!=""){
$filter['START_DATE'] = date("d/m/Y",  strtotime($filter['START_DATE']));
}
if($filter['END_DATE']!=""){
	$filter['END_DATE'] = date("d/m/Y",  strtotime($filter['END_DATE']));
}
$smarty->assign('cfer', $cfer);
$smarty->assign('bookList', $bookList);
$smarty->assign('booking', $booking);
$smarty->assign('technician', $technician);
$smarty->assign('sData', $serviceData);
$smarty->assign('pager', $pager);
$smarty->assign('filter', $filter);
$smarty->display('booking_list.html');